package com.scut.fy.admin.util;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
 * TODO
 *
 * @author JY
 * @version 1.0
 * @date 2021/9/3
 */
public class MysqlGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
    public static void main(String[] args) {

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig.Builder(
                "jdbc:mysql://127.0.0.1:3306/forever_young?useUnicode=true&useSSL=true&characterEncoding=utf-8",
                "root",
                "1qaz2wsx")
                .build();
        // 全局配置
        GlobalConfig gc = new GlobalConfig.Builder()
                .outputDir(System.getProperty("user.dir")+"/src/main/java")
                .author("jy")
                .openDir(false)
                .build();
        // 包配置
        PackageConfig pc = new PackageConfig.Builder()
                .parent("com.scut.fy")
                .moduleName(scanner("模块名"))
                .build();


        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig.Builder()
                .build();
        // 策略配置
        StrategyConfig strategy = new StrategyConfig.Builder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .entityBuilder().enableLombok()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .controllerBuilder().enableRestStyle()
                .build();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc)
                .global(gc)
                .packageInfo(pc)
                .template(templateConfig)
                .strategy(strategy);
        // 执行代码生成器
        mpg.execute();

    }

}
